package com.sx.micowell.module.splash

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.rx.RxTimer
import com.example.mvvm.until.SPUtils
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.AppBanner
import com.miloyu.modelnet.response.getUrl
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.sx.micowell.R
import com.sx.micowell.databinding.ActSplashBinding
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.app.MyApplication
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.model.HomeModel
import com.sx.micowell.framework.model.MainModel
import com.sx.micowell.framework.model.SplashModel
import com.uuzuche.lib_zxing.activity.ZXingLibrary
import kotlinx.android.synthetic.main.act_splash.*
import tv.danmaku.ijk.media.player.IMediaPlayer

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/6/25 13:38
 */
class SplashActivity : MyActivity<ActSplashBinding>() {

    private val model: SplashModel by lazy {
        getActivityScopeViewModel(SplashModel::class.java)
    }

    private  var player: PlayerView ?= null

    override fun getDataBindingConfig(): DataBindingConfig = DataBindingConfig(R.layout.act_splash)

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        model.mpUrl.observe(this) {
            //缓存
            if ("0".equals(it.info.bannerAttribute)) {
                startVideoPlayer(it.info.url.getUrl(), null)
                player!!.setOnInfoListener(object : IMediaPlayer.OnInfoListener {
                    override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
                        if (p1 == PlayStateParams.STATE_COMPLETED) {
                            //结束
                            player!!.stopPlay()
                            intentMain()
                        }
                        return true
                    }
                })
            } else {
                player!!.hideVideView(true)
                startVideoPlayer(null, it.info.cover)
                imageLoad()

            }
        }
    }

    override fun initData() {
        if (!TextUtils.isEmpty(SPUtils.getInstance().getString(SPKey.SP_TOKEN))) {
            RouteManager.getInstance().intentSportActivity()
            finish()
            return
        }
        initVideoPlayer()
        HANDLER.post {
            var url = SPUtils.getInstance().getString("urlJson")
            if (!TextUtils.isEmpty(url)) {
                var fromJson = Gson().fromJson<AppBanner>(url, AppBanner::class.java)
                model.mpUrl.value = fromJson
                model.getUrl(true)
            } else {
                model.getUrl(false)
            }
        }

    }

    override fun getTitleBar(): TitleBar? {
        return null
    }

    /**
     * 初始化视频播放器
     */
    private fun initVideoPlayer() {
        player = PlayerView(this)
            .setScaleType(PlayStateParams.fitparent)
            .setShowSpeed(false)
            .forbidTouch(true)
            .hideAllUI()
            .hideHideTopBar(true)
            .hideSteam(true)
            .hideFullscreen(true)
            .hideRotation(true)
            .hideCenterPlayer(true)
            .hideBottonBar(true)
    }

    /**
     * 开始播放视频
     */
    private fun startVideoPlayer(mp4: String?, bg: String?) {

        player?.showThumbnail { ivThumbnail ->
            Glide.with(this@SplashActivity)
                .load(bg)
                .placeholder(R.drawable.bg)
                .error(R.drawable.bg)
                .into(ivThumbnail)
        }

        mp4?.let {
            player?.setPlaySource(
                MyApplication.instance().getProxy(this@SplashActivity)
                    ?.getProxyUrl(mp4)
            )?.startPlay()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        player?.let {
            it.stopPlay()
        }
    }

    fun intentMain() {
        if (TextUtils.isEmpty(SPUtils.getInstance().getString(SPKey.SP_TOKEN))) {
            RouteManager.getInstance().intentLogin()
        } else {
            RouteManager.getInstance().intentSportActivity()
        }
        finish()
    }

    fun imageLoad() {
        val alphaAnimation = AlphaAnimation(0.5f, 1.0f)
        alphaAnimation.duration = 500
        (findViewById(R.id.iv_trumb) as ImageView).animation = alphaAnimation;
        alphaAnimation.start()
        alphaAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                intentMain()
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

        })

    }

    override fun onClick(v: View?) {
        super.onClick(v)
        intentMain()
    }
}


